package com.bytecode.javassist;

import javassist.*;

import java.io.IOException;

/**
 * 描述:
 *
 * @author luzhongfu
 * Created by lucky on 2020-07-02 1:49 PM
 */
public class JavassistTest {

    public static void main(String[] args) throws NotFoundException, CannotCompileException, IOException, IllegalAccessException, InstantiationException {
        ClassPool classPool = ClassPool.getDefault();
        CtClass ctClass = classPool.get("com.bytecode.javassist.Base");

        CtMethod method = ctClass.getDeclaredMethod("process");
        method.insertBefore("{ System.out.println(\"start\"); }");
        method.insertAfter("{ System.out.println(\"end\"); }");
        Class<?> aClass = ctClass.toClass();

        ctClass.writeFile("/Users/luzhongfu/javassist");

        Base o = (Base) aClass.newInstance();
        o.process();
    }
}
